# Maintainer: Lucio Perusini <lucioperusini@gmail.com>

_realname=sqlitebrowser
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.12.2
pkgrel=3
pkgdesc='SQLite Database browser is a Qt GUI editor for SQLite databases (mingw-w64)'
url='https://sqlitebrowser.org/'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/sqlitebrowser/sqlitebrowser/archive/v${pkgver}.tar.gz"
        "0001-sqlitebrowser-install.patch"
        "0002-sqlitebrowser-linking.patch")
sha256sums=('645f98d38e5d128a449e93cebf03c8070f9eacd2d16b10e433a781d54a9d478a'
            '340b343f2d86bae6f4666d77ed16f7c2d1f2986370833d4ecd000b6835a0c0b9'
            '826845aae4fa5312d057989b929ef45473c388c6636e8ad7d018c9e1ca591349')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/0001-sqlitebrowser-install.patch
  patch -Np1 -i "${srcdir}"/0002-sqlitebrowser-linking.patch
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_STABLE_VERSION=ON \
    -Wno-dev \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
